package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.SysRoleModules;
import com.sxhalo.fsms.service.ISysRoleModulesService;
import com.sxhalo.fsms.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api")
public class SysRoleModuleController extends BaseController {

	@Autowired
	private ISysRoleModulesService rolemoduleService;

	@ResponseBody
	@RequestMapping(value = "/role/module", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findModule(Integer roleId, HttpServletRequest req) {
		List<String> ids = new ArrayList<String>();
		List<SysRoleModules> roleModule = rolemoduleService.findWhereList("role_id =" + roleId, "");
		for (SysRoleModules rm : roleModule) {
			ids.add(rm.getModuleId().toString());
		}
		return ApiResult.retOK(req, ids);
	}

	@ResponseBody
	@RequestMapping(value = "/role/module", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult saveModule(String modules, Integer roleId, HttpServletRequest req) {

		rolemoduleService.deleteByRoleId(roleId);
		if(!modules.equals("")){
			String[] mids = modules.split(",");
			rolemoduleService.batchInsert(mids, roleId);
		}
		return ApiResult.retOK(req);
	}


}
